<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/18 9:37
 */

namespace app\admin\controller;


use think\facade\View;
use app\admin\business\Manager as BusManager;
use app\admin\business\Role as BusRole;
use app\admin\validate\Manager as ManagerValidate;

class Manager extends AdminBase
{

    /**
     * 功能说明 菜单栏首页
     * @author: wang
     * @Date: 2020/3/23 22:28
     * @return string|\think\response\Json
     * @throws \Exception
     */
    public function index()
    {

        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit', '', 'trim,htmlspecialchars') ?? 15;
            $username = $this->request->param('username', '', 'trim,htmlspecialchars');
            $where = [];
            if (!empty($username) && $username != '') {
                $where[] = ['username', 'like', "%" . $username . "%"];
            }
            $list = BusManager::getManagerList($where, $limit);
            return $this->show(config('status.lay_table'), 'success', $list);
        }

        return View::fetch('index');
    }

    /**
     * 功能说明 添加菜单栏
     * @author: wang
     * @Date: 2020/3/23 22:28
     * @return string|\think\response\Json
     * @throws \Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $username = $this->request->param('username', '', 'trim,htmlspecialchars');
            $password = $this->request->param('password', '', 'trim,htmlspecialchars');
            $real_name = $this->request->param('real_name', '', 'trim,htmlspecialchars');
            $role_id = $this->request->param('role_id', '', 'trim,htmlspecialchars');
            $icon = $this->request->param('icon', '', 'trim,htmlspecialchars');
            $status = $this->request->param('status', '', 'trim,htmlspecialchars') ?? 1;
            $data = [
                'username' => $username,
                'password' => $password,
                'real_name' => $real_name,
                'role_id' => $role_id,
                'icon' => $icon,
                'status' => $status,
                'last_login_time' => time(),
                'last_login_ip' => request()->ip(),
            ];
            $ManagerValidate = new ManagerValidate();
            if (!$ManagerValidate->scene('add')->check($data)) {
                return $this->show(config('status.error'), $ManagerValidate->getError());
            }

            try {
                $result = BusManager::add($data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }

            if ($result == true) {
                return $this->result(config('status.success'), '添加成功', '/admin/manager/index');
            } else {
                return $this->show(config('status.error'), '添加失败');
            }
        }

        $data = BusRole::getAllRole();
        return View::fetch('save', [
            'roleData' => $data
        ]);
    }

    /**
     * 功能说明 通过ID更新角色
     * @author: wang
     * @Date: 2020/3/24 18:06
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $id = $this->request->param('id', '', 'trim,htmlspecialchars');
        if ($this->request->isPost()) {
            $username = $this->request->param('username', '', 'trim,htmlspecialchars');
            $password = $this->request->param('password', '', 'trim,htmlspecialchars');
            $real_name = $this->request->param('real_name', '', 'trim,htmlspecialchars');
            $role_id = $this->request->param('role_id', '', 'trim,htmlspecialchars');
            $icon = $this->request->param('icon', '', 'trim,htmlspecialchars');
            $status = $this->request->param('status', '', 'trim,htmlspecialchars') ?? 1;

            $data = [
                'id' => $id,
                'username' => $username,
                'password' => $password,
                'real_name' => $real_name,
                'role_id' => $role_id,
                'icon' => $icon,
                'status' => $status,
                'last_login_time' => time(),
                'last_login_ip' => request()->ip(),
            ];
            $ManagerValidate = new ManagerValidate();
            if (!$ManagerValidate->scene('edit')->check($data)) {
                return $this->show(config('status.error'), $ManagerValidate->getError());
            }

            try {
                $result = BusManager::edit($data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }
            if ($result !== false) {
                return $this->result(config('status.success'), '更新成功', '/admin/manager/index');
            } else {
                return $this->show(config('status.error'), '更新失败');
            }
        }

        $data = BusManager::getDataById($id);
        $roleData = BusRole::getAllRole();
        if(!empty($data['icon'])){
            $data['icon'] = '/'.$data['icon'];
        }
        return View::fetch('save', [
            'data' => $data,
            'roleData' => $roleData,
        ]);
    }

    /**
     * 功能说明 删除数据
     * @author: wang
     * @Date: 2020/3/25 20:50
     * @return \think\response\Json
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $type = $this->request->param('type', '', 'trim,htmlspecialchars');
            if (!empty($type) && $type == 'all-del') {
                $ids = $this->request->param('ids', '', 'trim,htmlspecialchars');
                try {
                    $result = BusManager::delDataByIds($ids);
                } catch (\Exception $e) {
                    return $this->show(config('status.error'), $e->getMessage());
                }
                if ($result !== false) {
                    return $this->result(config('status.success'), '删除成功', '/admin/manager/index');
                } else {
                    return $this->show(config('status.error'), '删除失败');
                }
            } else {
                $id = $this->request->param('id', '', 'trim,htmlspecialchars');
                $data = [
                    'id' => $id,
                ];
                $ManagerValidate = new ManagerValidate();
                if (!$ManagerValidate->scene('del')->check($data)) {
                    return $this->show(config('status.error'), $ManagerValidate->getError());
                }
                try {
                    $result = BusManager::delDataById($id);
                } catch (\Exception $e) {
                    return $this->show(config('status.error'), $e->getMessage());
                }

                if ($result !== false) {
                    return $this->result(config('status.success'), '删除成功', '/admin/manager/index');
                } else {
                    return $this->show(config('status.error'), '删除失败');
                }
            }

        }
    }


    /**
     * 功能说明 更新状态
     * @author: wang
     * @Date: 2020/3/25 18:10
     * @return \think\response\Json
     */
    public function upStatus()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', '', 'trim,htmlspecialchars');
            $status = $this->request->param('status', '', 'trim,htmlspecialchars');
            if (!empty($id) && !empty($status) && ($status == 'on' || $status == 'off')) {
                if ($status == 'on') {
                    $status = 1;
                } elseif ($status == 'off') {
                    $status = 0;
                }

                $ManagerValidate = new ManagerValidate();
                $data = [
                    'id' => $id,
                    'status' => $status
                ];
                if (!$ManagerValidate->scene('status')->check($data)) {
                    return $this->show(config('status.error'), $ManagerValidate->getError());
                }

                try {
                    $result = BusManager::upStatus($data);
                    if ($result !== false) {
                        return $this->show(config('status.success'), '更新成功');
                    } else {
                        return $this->show(config('status.error'), '更新失败');
                    }
                } catch (\Exception $e) {
                    return $this->show(config('status.error'), $e->getMessage());
                }
            } else {
                return $this->show(config('status.error'), '参数错误');
            }

        } else {
            return $this->show(config('status.error'), '请求类型错误');
        }

    }




}